package com.liukai.novel_rack.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_base.base.BaseActivity;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.utils.JSONUtil;
import com.liukai.novel_info.ui.NovelDescActivity;
import com.liukai.novel_rack.R;
import com.liukai.novel_rack.R2;
import com.liukai.novel_rack.adapter.SearchResultAdapter;
import com.liukai.novel_rack.call_back.ISearchCallBack;
import com.liukai.novel_rack.presenter.impl.SearchPresenterImpl;
import com.liukai.novel_rack.search_interface.OnSearchItemListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SearchResultActivity extends BaseActivity implements ISearchCallBack {

    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;

    @BindView(R2.id.btn_back)
    TextView btnBack;

    @BindView(R2.id.et_search_content)
    EditText searchContent;

    @BindView(R2.id.btn_search)
    TextView btnSearch;

    private SearchResultAdapter mSearchResultAdapter;
    private String mSearchContent;
    private SearchPresenterImpl mSearchPresenter;
    private List<NovelInfo> mNovelInfoList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initData(Context context) {
        Intent intent = getIntent();
        if (intent == null) return;
        mSearchContent = intent.getStringExtra("searchContent");
        searchContent.setText(mSearchContent);
        mSearchPresenter = new SearchPresenterImpl();
        mSearchPresenter.registerCallback(this);
        mSearchPresenter.findNovelLikeName(mSearchContent);
        mNovelInfoList = new ArrayList<>();

    }

    @Override
    protected void initView() {

        mSearchResultAdapter = new SearchResultAdapter(mNovelInfoList, this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mSearchResultAdapter);

    }

    @Override
    protected void initListener() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = searchContent.getText().toString();
                mSearchPresenter.findNovelLikeName(content);
            }
        });

        mSearchResultAdapter.setOnSearchItemListener(new OnSearchItemListener() {
            @Override
            public void onItemClick(NovelInfo novelInfo) {
                // TODO 搜索出来的点击事件
                Intent intent = new Intent(SearchResultActivity.this, NovelDescActivity.class);
                intent.putExtra("novelInfo",novelInfo);
                startActivity(intent);
            }
        });
    }

    @Override
    public void findNovelLikeNameResult(Result result) {
        if (result.getCode() != 200) return;
        List<NovelInfo> novelInfoList = JSONUtil.resolverResultList(result, NovelInfo.class);
        mSearchResultAdapter.setNovelInfoList(novelInfoList);
    }
}
